package com.java.servlet;

import com.java.util.CookieUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author jingjintao
 * @date 2021/6/19 - 23:32
 **/
public class CookieServlet extends BaseServlet {

    protected void testPath(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Cookie cookie = new Cookie("path", "path");
        //getContextPath():得到工程路径
        cookie.setPath(req.getContextPath()+"/abc");// /工程路径/abc
        resp.addCookie(cookie);
        resp.getWriter().write("创建了一个带有Path路径的Cookie");
    }

    protected void life3600(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Cookie cookie = new Cookie("life3600", "life3600");
        cookie.setMaxAge(60 * 60); // 设置Cookie一小时之后被删除。无效
        resp.addCookie(cookie);
        resp.getWriter().write("已经创建了一个存活一小时的Cookie");
    }

    /**
     * 马上删除一个cookie
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Cookie cookie = CookieUtils.findCookie("key2", req.getCookies());
        if (cookie != null) {
            cookie.setMaxAge(0);
            resp.addCookie(cookie);
            resp.getWriter().write("key2的cookie已经被删除");
        }
    }

    /**
     * 默认的会话级别的Cookie
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Cookie cookie = new Cookie("defaultLife", "defaultLife");
        cookie.setMaxAge(-1);//设置存活时间
        resp.addCookie(cookie);
    }

    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //方式一
        // 1、先创建一个要修改的同名的Cookie对象
//         2、在构造器，同时赋于新的Cookie值。
//        Cookie cookie = new Cookie("key1","newValue1");
        //3、调用response.addCookie( Cookie ); 通知 客户端 保存修改
//        resp.addCookie(cookie);
//

        //方式二
        //1、先查找到需要修改的Cookie对象
        Cookie cookie = CookieUtils.findCookie("key2", req.getCookies());
        if (cookie != null) {
//            2、调用setValue()方法赋于新的Cookie值。
            cookie.setValue("newValue2");
            //3、调用response.addCookie()通知客户端保存修改
            resp.addCookie(cookie);
        }

        resp.getWriter().write("key1的cookie已经修改好了");
    }

    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            // getName方法返回Cookie的key（名）
            // getValue方法返回Cookie的value值
            resp.getWriter().write("Cookie[" + cookie.getName() + " = " + cookie.getValue() + " ] <br/>");
        }

        Cookie iWantCookie = CookieUtils.findCookie("key1", cookies);
//        for (Cookie cookie : cookies) {
//
//            if("key2".equals(cookie.getName())){
//                iWantCookie = cookie;
//            }
//        }

        if (iWantCookie != null) {
            resp.getWriter().write("找到了cookie");
        }

    }

    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1.创建Cookie对象
        Cookie cookie = new Cookie("key2", "val2");
        //2.通知客户端保存Cookie
        resp.addCookie(cookie);
        //1.创建Cookie对象
        Cookie cookie1 = new Cookie("key3", "val3");
        //2.通知客户端保存Cookie
        resp.addCookie(cookie1);
        resp.getWriter().write("Cookie创建成功");
    }
}
